'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: account_handler.py
* @Time: 2025/10/11
* @All Rights Reserve By Brtc
'''
from dataclasses import dataclass

from flask_login import login_required, current_user
from injector import inject

from internal.schema.account_schema import GetCurrentUserResp, UpdatePasswordReq, UpdateAvatarReq, UpdateNameReq
from internal.service import AccountService
from pkg.response import success_json, validate_error_json, success_message


@inject
@dataclass
class AccountHandler:
    """账号设置处理器"""
    account_service:AccountService

    @login_required
    def get_current_user(self):
        """获取当前用户登录信息"""
        resp = GetCurrentUserResp()
        return success_json(resp.dump(current_user))


    @login_required
    def update_password(self):
        """更新当前账户的密码"""
        #1、提取数据并校验
        req = UpdatePasswordReq()

        if not req.validate:
            return validate_error_json(req.errors)

        #2、调用服务更新账号密码
        self.account_service.update_password(req.password.data, current_user)
        return success_message("更新密码成功！")


    @login_required
    def update_name(self):
        """更新当前用户名"""
        # 1、提取数据并校验
        req = UpdateNameReq()

        if not req.validate:
            return validate_error_json(req.errors)

        # 2、调用服务更新账号密码
        self.account_service.update_account(current_user, name = req.name.data)
        return success_message("更新用户名成功！")


    @login_required
    def update_avatar(self):
        """更新头像"""
        # 1、提取数据并校验
        req = UpdateAvatarReq()

        if not req.validate:
            return validate_error_json(req.errors)

        # 2、调用服务更新账号密码
        self.account_service.update_account(current_user, avatar=req.avatar.data)
        return success_message("更新用户头像成功！")
